home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src-tk / tkdos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-07  |  32.3 KB  |  1,393 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. // Mesa Tweaking by: Mark E. Peterson (markp@ic.mankato.mn.us)
  39. // Adapted from windows version for dos by: Charlie Wallace (cwallace@dreamworks.com)
  40.  
  41.  
  42. // Force this on, i`m always forgetting, and it must be on for this
  43. // file anyway, cw..
  44.  
  45. #ifndef DOSVGA
  46. #define DOSVGA 1
  47. #endif
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <stdarg.h>
  53. #include <conio.h>
  54.  
  55. #ifdef DJGPP
  56. #include <pc.h>
  57. #endif
  58.  
  59. // Ah the wonders of TRUE and FALSE, heaven help us if they`re enums
  60. #ifndef FALSE
  61. #define FALSE 0
  62. #endif
  63.  
  64. #ifndef TRUE
  65. #define TRUE 1
  66. #endif
  67.  
  68. #include "gltk.h"
  69. #include "gl\dosmesa.h"
  70.  
  71. #define static
  72.  
  73. #if defined(__cplusplus) || defined(c_plusplus)
  74. #define class c_class
  75. #endif
  76.  
  77. #if DBG
  78. #define TKASSERT(x)                                     \
  79. if ( !(x) ) {                                           \
  80.     PrintMessage("%s(%d) Assertion failed %s\n",        \
  81.         __FILE__, __LINE__, #x);                        \
  82. }
  83. #else
  84. #define TKASSERT(x)
  85. #endif  /* DBG */
  86.  
  87. /******************************************************************************/
  88.  
  89. static struct _WINDOWINFO {
  90.     int x, y;
  91.     int width, height;
  92.     GLenum type;
  93.     GLenum dmPolicy;
  94.     int ipfd;
  95.     BOOL bDefPos;
  96. } windInfo = {
  97.     0, 0, 320, 200, TK_INDEX | TK_SINGLE, TK_MINIMUM_CRITERIA, 0, TRUE
  98. };
  99.  
  100.  
  101. static HWND     tkhwnd     = NULL;
  102. static HDC      tkhdc      = NULL;
  103. static HPALETTE tkhpalette = NULL;
  104. GLboolean tkPopupEnable = TRUE;
  105.  
  106. // Fixed palette support.
  107.  
  108. #define BLACK   PALETTERGB(0,0,0)
  109. #define WHITE   PALETTERGB(255,255,255)
  110. #define NUM_STATIC_COLORS   (COLOR_BTNHIGHLIGHT - COLOR_SCROLLBAR + 1)
  111.  
  112. static void (*ExposeFunc)(int, int)              = NULL;
  113. static void (*ReshapeFunc)(GLsizei, GLsizei)     = NULL;
  114. static void (*DisplayFunc)(void)                 = NULL;
  115. static GLenum (*KeyDownFunc)(int, GLenum)        = NULL;
  116. static GLenum (*MouseDownFunc)(int, int, GLenum) = NULL;
  117. static GLenum (*MouseUpFunc)(int, int, GLenum)   = NULL;
  118. static GLenum (*MouseMoveFunc)(int, int, GLenum) = NULL;
  119. static void (*IdleFunc)(void)                    = NULL;
  120.  
  121. static char     *lpszClassName = "tkLibWClass";
  122. static WCHAR    *lpszClassNameW = "tkLibWClass";
  123.  
  124. long tkWndProc(HWND hWnd, UINT message, DWORD wParam, LONG lParam);
  125. static unsigned char ComponentFromIndex(int i, int nbits, int shift );
  126. static void PrintMessage( const char *Format, ... );
  127. //static PALETTEENTRY *FillRgbPaletteEntries( PIXELFORMATDESCRIPTOR *Pfd, PALETTEENTRY *Entries, UINT Count );
  128. static HPALETTE CreateCIPalette( HDC Dc );
  129. static HPALETTE CreateRGBPalette( HDC hdc );
  130. static void DestroyThisWindow( HWND Window );
  131. static void CleanUp( void );
  132. static void DelayPaletteRealization( void );
  133. static long RealizePaletteNow( HDC Dc, HPALETTE Palette);
  134. static void ForceRedraw( HWND Window );
  135. static void *AllocateMemory( size_t Size );
  136. static void *AllocateZeroedMemory( size_t Size );
  137. static void FreeMemory( void *Chunk );
  138.  
  139. /*
  140.  *  Prototypes for the debugging functions go here
  141.  */
  142.  
  143. #define DBGFUNC 0
  144. #if DBGFUNC
  145.  
  146. static void DbgPrintf( const char *Format, ... );
  147. static void pwi( void );
  148. static void pwr(RECT *pr);
  149. //static void ShowPixelFormat(HDC hdc);
  150.  
  151. #endif
  152. #define NCOLORS 17
  153. float tkRGBMap[NCOLORS][3] = {
  154.     {0,0,0},
  155.     {0,0,0},
  156.     {0,0,0},
  157.     {0,0,0},
  158.     {0,0,0},
  159.     {0,0,0},
  160.     {0,0,0},
  161.     {0,0,0},
  162.     {0,0,0},
  163.     {0,0,0},
  164.     {1,0,0},
  165.     {0,1,0},
  166.     {1,1,0},
  167.     {0,0,1},
  168.     {1,0,1},
  169.     {0,1,1},
  170.     {1,1,1}
  171. };
  172.  
  173.  
  174. /***************************************************************
  175.  *                                                             *
  176.  *  Exported Functions go here                                 *
  177.  *                                                             *
  178.  ***************************************************************/
  179.  
  180. void tkErrorPopups(GLboolean bEnable)
  181. {
  182.     tkPopupEnable = bEnable;
  183. }
  184.  
  185. void tkCloseWindow(void)
  186. {
  187. #if 0
  188.     DestroyThisWindow(tkhwnd);
  189.     /* if (w.cMain) {
  190.        XMesaDestroyContext(w.cMain);
  191.     }*/
  192. #endif
  193.     DOSMesaDestroyContext(NULL);
  194. }
  195.  
  196.  
  197. void tkExec(void)
  198. {
  199.     int key;
  200.     char ch;
  201.     GLenum mask=0;
  202.  
  203.     //MSG Message;
  204.  
  205.     /*
  206.      *  WM_SIZE gets delivered before we get here!
  207.      */
  208.  
  209.     if (ReshapeFunc)
  210.     {
  211.         (*ReshapeFunc)(windInfo.width, windInfo.height);
  212.     }
  213.  
  214.     while (ch!=27)  //GL_TRUE
  215.     {
  216.         /*
  217.          *  Process all pending messages
  218.          */
  219.           #if 0
  220.         while (PeekMessage(&Message, NULL, 0, 0, PM_NOREMOVE) == TRUE)
  221.         {
  222.             if (GetMessage(&Message, NULL, 0, 0) )
  223.             {
  224.                 TranslateMessage(&Message);
  225.                 DispatchMessage(&Message);
  226.             }
  227.             else
  228.             {
  229.                 /*
  230.                  *  Nothing else to do here, just return
  231.                  */
  232.  
  233.                 return;
  234.             }
  235.         }
  236.         #endif
  237.  
  238.         /*
  239.          *  If an idle function was defined, call it
  240.          */
  241.  
  242.               if(kbhit()) {
  243.                  ch = getch();
  244.  
  245.                 if(KeyDownFunc) {
  246.                     switch(ch) {
  247.                         case ' ':
  248.                             key = TK_SPACE;
  249.                             break;
  250.                         case 27:
  251.                             key = TK_ESCAPE;
  252.                             break;
  253.                         case '1':
  254.                             key = TK_1;
  255.                             break;
  256.                         case '2':
  257.                             key = TK_2;
  258.                             break;
  259.                         case '3':
  260.                             key = TK_3;
  261.                             break;
  262.                         case '4':
  263.                             key = TK_4;
  264.                             break;
  265.                         case '5':
  266.                             key = TK_5;
  267.                             break;
  268.                         case '6':
  269.                             key = TK_6;
  270.                             break;
  271.                         case '7':
  272.                             key = TK_7;
  273.                             break;
  274.                         case '8':
  275.                             key = TK_8;
  276.                             break;
  277.                         case '9':
  278.                             key = TK_9;
  279.                             break;
  280.                         case '0':
  281.                             key = TK_0;
  282.                             break;
  283.                         case 'a':
  284.                             key = TK_a;
  285.                             break;
  286.                         case 'b':
  287.                             key = TK_b;
  288.                             break;
  289.                         case 'c':
  290.                             key = TK_c;
  291.                             break;
  292.                         case 'd':
  293.                             key = TK_d;
  294.                             break;
  295.                         case 'e':
  296.                             key = TK_e;
  297.                             break;
  298.                         case 'f':
  299.                             key = TK_f;
  300.                             break;
  301.                         case 'g':
  302.                             key = TK_g;
  303.                             break;
  304.                         case 'h':
  305.                             key = TK_h;
  306.                             break;
  307.                         case 'i':
  308.                             key = TK_i;
  309.                             break;
  310.                         case 'j':
  311.                             key = TK_j;
  312.                             break;
  313.                         case 'k':
  314.                             key = TK_k;
  315.                             break;
  316.                         case 'l':
  317.                             key = TK_l;
  318.                             break;
  319.                         case 'm':
  320.                             key = TK_m;
  321.                             break;
  322.                         case 'n':
  323.                             key = TK_n;
  324.                             break;
  325.                         case 'o':
  326.                             key = TK_o;
  327.                             break;
  328.                         case 'p':
  329.                             key = TK_p;
  330.                             break;
  331.                         case 'q':
  332.                             key = TK_q;
  333.                             break;
  334.                         case 'r':
  335.                             key = TK_r;
  336.                             break;
  337.                         case 's':
  338.                             key = TK_s;
  339.                             break;
  340.                         case 't':
  341.                             key = TK_t;
  342.                             break;
  343.                         case 'u':
  344.                             key = TK_u;
  345.                             break;
  346.                         case 'v':
  347.                             key = TK_v;
  348.                             break;
  349.                         case 'w':
  350.                             key = TK_w;
  351.                             break;
  352.                         case 'x':
  353.                             key = TK_x;
  354.                             break;
  355.                         case 'y':
  356.                             key = TK_y;
  357.                             break;
  358.                         case 'z':
  359.                             key = TK_z;
  360.                             break;
  361.                         case 'A':
  362.                             key = TK_A;
  363.                             break;
  364.                         case 'B':
  365.                             key = TK_B;
  366.                             break;
  367.                         case 'C':
  368.                             key = TK_C;
  369.                             break;
  370.                         case 'D':
  371.                             key = TK_D;
  372.                             break;
  373.                         case 'E':
  374.                             key = TK_E;
  375.                             break;
  376.                         case 'F':
  377.                             key = TK_F;
  378.                             break;
  379.                         case 'G':
  380.                             key = TK_G;
  381.                             break;
  382.                         case 'H':
  383.                             key = TK_H;
  384.                             break;
  385.                         case 'I':
  386.                             key = TK_I;
  387.                             break;
  388.                         case 'J':
  389.                             key = TK_J;
  390.                             break;
  391.                         case 'K':
  392.                             key = TK_K;
  393.                             break;
  394.                         case 'L':
  395.                             key = TK_L;
  396.                             break;
  397.                         case 'M':
  398.                             key = TK_M;
  399.                             break;
  400.                         case 'N':
  401.                             key = TK_N;
  402.                             break;
  403.                         case 'O':
  404.                             key = TK_O;
  405.                             break;
  406.                         case 'P':
  407.                             key = TK_P;
  408.                             break;
  409.                         case 'Q':
  410.                             key = TK_Q;
  411.                             break;
  412.                         case 'R':
  413.                             key = TK_R;
  414.                             break;
  415.                         case 'S':
  416.                             key = TK_S;
  417.                             break;
  418.                         case 'T':
  419.                             key = TK_T;
  420.                             break;
  421.                         case 'U':
  422.                             key = TK_U;
  423.                             break;
  424.                         case 'V':
  425.                             key = TK_V;
  426.                             break;
  427.                         case 'W':
  428.                             key = TK_W;
  429.                             break;
  430.                         case 'X':
  431.                             key = TK_X;
  432.                             break;
  433.                         case 'Y':
  434.                             key = TK_Y;
  435.                             break;
  436.                         case 'Z':
  437.                             key = TK_Z;
  438.                             break;
  439.                         case 0:
  440.                             if( kbhit() ) {
  441.                                 ch = getch();
  442.                                 switch( ch ) {
  443.                                     case 'P':
  444.                                         key = TK_UP;
  445.                                         break;
  446.                                     case 'M':
  447.                                         key = TK_LEFT;
  448.                                         break;
  449.                                     case 'K':
  450.                                         key = TK_RIGHT;
  451.                                         break;
  452.                                     case 'H':
  453.                                         key = TK_DOWN;
  454.                                         break;
  455.                                 }
  456.                             }
  457.                             break;
  458.                         }
  459.                         (*KeyDownFunc)(key,mask);
  460.                         key = 0;
  461.                     }
  462.                 }
  463.  
  464.            if (IdleFunc)
  465.         {
  466.             (*IdleFunc)();
  467.         }
  468.         if( DisplayFunc)
  469.         {
  470.             (*DisplayFunc)();
  471.         }
  472.     }
  473. }
  474.  
  475. void tkExposeFunc(void (*Func)(int, int))
  476. {
  477.     ExposeFunc = Func;
  478. }
  479.  
  480. void tkReshapeFunc(void (*Func)(GLsizei, GLsizei))
  481. {
  482.     ReshapeFunc = Func;
  483. }
  484.  
  485. void tkDisplayFunc(void (*Func)(void))
  486. {
  487.     DisplayFunc = Func;
  488. }
  489.  
  490. void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
  491. {
  492.     KeyDownFunc = Func;
  493. }
  494.  
  495. void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
  496. {
  497.     MouseDownFunc = Func;
  498. }
  499.  
  500. void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
  501. {
  502.     MouseUpFunc = Func;
  503. }
  504.  
  505. void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
  506. {
  507.     MouseMoveFunc = Func;
  508. }
  509.  
  510. void tkIdleFunc(void (*Func)(void))
  511. {
  512.     IdleFunc = Func;
  513.  
  514. }
  515.  
  516. #ifdef DOSVGA
  517. extern int vga_getxdim(void);
  518. extern int vga_getydim(void);
  519. #endif
  520.  
  521. void tkInitPosition(int x, int y, int width, int height)
  522. {
  523. #ifdef DOSVGA
  524.     if(width>vga_getxdim()) width = vga_getxdim();
  525.     if(height>vga_getydim()) height = vga_getydim();
  526. #endif
  527.  
  528.    windInfo.bDefPos = FALSE;
  529.    windInfo.x = x ;
  530.    windInfo.y = y ;
  531.    windInfo.width = width;
  532.    windInfo.height = height;
  533. }
  534.  
  535. void tkInitDisplayMode(GLenum type)
  536. {
  537.     windInfo.type = type;
  538. }
  539.  
  540. void tkInitDisplayModePolicy(GLenum type)
  541. {
  542.     windInfo.dmPolicy = type;
  543. }
  544.  
  545. GLenum tkInitDisplayModeID(GLint ipfd)
  546. {
  547.     windInfo.ipfd = ipfd;
  548.     return GL_TRUE;
  549. }
  550.  
  551. extern unsigned short vga_cindex ;
  552.  
  553.  
  554. extern void restore_video_mode(void); /* see dosmesa.c */
  555. extern void set_video_mode(unsigned short,unsigned short,char); /* see dosmesa.c */
  556.  
  557. void resetcrt(void)
  558. {
  559.     restore_video_mode();
  560. }
  561. GLenum tkInitWindowAW(char *title, BOOL bUnicode)
  562. {
  563.     DOSMesaContext Cur;
  564.     GLenum   Result = GL_FALSE,RGB_Flag=GL_TRUE,DB_Flag=GL_FALSE;
  565.     if( windInfo.type & TK_INDEX )
  566.         set_video_mode(windInfo.x,windInfo.y,FALSE);
  567.      else
  568.          set_video_mode(windInfo.x,windInfo.y,TRUE);
  569.  
  570.     atexit( resetcrt );
  571.  
  572. #if 0
  573.     WNDCLASS wndclass;
  574.     RECT     WinRect;
  575.     HANDLE   hInstance;
  576.     ATOM     aRegister;
  577.  
  578.     hInstance = GetModuleHandle(NULL);
  579.  
  580.     // Must not define CS_CS_PARENTDC style.
  581.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  582.     wndclass.lpfnWndProc   = (WNDPROC)tkWndProc;
  583.     wndclass.cbClsExtra    = 0;
  584.     wndclass.cbWndExtra    = 0;
  585.     wndclass.hInstance     = hInstance;
  586.     wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  587.     wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  588.     wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
  589.     wndclass.lpszMenuName  = NULL;
  590.  
  591.     if (bUnicode)
  592.         wndclass.lpszClassName = (LPCSTR)lpszClassNameW;
  593.     else
  594.         wndclass.lpszClassName = (LPCSTR)lpszClassName;
  595.  
  596.     if (bUnicode)
  597.     {
  598.         aRegister = RegisterClassW((CONST WNDCLASSW *)&wndclass);
  599.     }
  600.     else
  601.     {
  602.         aRegister = RegisterClass(&wndclass);
  603.     }
  604.  
  605.  
  606.     /*
  607.      *  If the window failed to register, then there's no
  608.      *  need to continue further.
  609.      */
  610.  
  611.     if(0 == aRegister)
  612.     {
  613.         PrintMessage("Failed to register window class\n");
  614.         return(Result);
  615.     }
  616.  
  617.  
  618.     /*
  619.      *  Make window large enough to hold a client area as large as windInfo
  620.      */
  621.  
  622.     WinRect.left   = windInfo.x;
  623.     WinRect.right  = windInfo.x + windInfo.width;
  624.     WinRect.top    = windInfo.y;
  625.     WinRect.bottom = windInfo.y + windInfo.height;
  626.  
  627.     AdjustWindowRect(&WinRect, WS_OVERLAPPEDWINDOW, FALSE);
  628.  
  629.     /*
  630.      *  Must use WS_CLIPCHILDREN and WS_CLIPSIBLINGS styles.
  631.      */
  632.  
  633.     if (bUnicode)
  634.     {
  635.         tkhwnd = CreateWindowW(
  636.                     (LPCWSTR)lpszClassNameW,
  637.                     (LPCWSTR)title,
  638.                     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  639.                     (windInfo.bDefPos) ? CW_USEDEFAULT : WinRect.left,
  640.                     (windInfo.bDefPos) ? CW_USEDEFAULT : WinRect.top,
  641.                     WinRect.right - WinRect.left,
  642.                     WinRect.bottom - WinRect.top,
  643.                     NULL,
  644.                     NULL,
  645.                     hInstance,
  646.                     NULL);
  647.     }
  648.     else
  649.     {
  650.         tkhwnd = CreateWindow(
  651.                     lpszClassName,
  652.                     title,
  653.                     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  654.                     (windInfo.bDefPos) ? CW_USEDEFAULT : WinRect.left,
  655.                     (windInfo.bDefPos) ? CW_USEDEFAULT : WinRect.top,
  656.                     WinRect.right - WinRect.left,
  657.                     WinRect.bottom - WinRect.top,
  658.                     NULL,
  659.                     NULL,
  660.                     hInstance,
  661.                     NULL);
  662.     }
  663.  
  664.     if ( NULL != tkhwnd )
  665.     {
  666.         // If default window positioning used, find out window position and fix
  667.         // up the windInfo position info.
  668.  
  669.         if (windInfo.bDefPos)
  670.         {
  671.             GetWindowRect(tkhwnd, &WinRect);
  672.             windInfo.x = WinRect.left + GetSystemMetrics(SM_CXFRAME);
  673.             windInfo.y = WinRect.top  + GetSystemMetrics(SM_CYCAPTION)
  674.                          - GetSystemMetrics(SM_CYBORDER)
  675.                          + GetSystemMetrics(SM_CYFRAME);
  676.         }
  677.  
  678.         tkhdc = GetDC(tkhwnd);
  679.  
  680.         if ( NULL != tkhdc )
  681.             ShowWindow(tkhwnd, SW_SHOWDEFAULT);
  682.         else
  683.             PrintMessage("Could not get an HDC for window 0x%08lX\n", tkhwnd );
  684.     }
  685.     else
  686.         PrintMessage("create window failed\n");
  687. #endif
  688.  
  689.  
  690.     if (windInfo.type & TK_INDEX)
  691.     {
  692.         vga_cindex = 256 ;
  693.       RGB_Flag=GL_FALSE;
  694.       tkSetRGBMap(NCOLORS,(float *) tkRGBMap);
  695.     }
  696.     if (windInfo.type & TK_DOUBLE)
  697.       DB_Flag=GL_TRUE;
  698.  
  699.     Cur=DOSMesaCreateContext();
  700.     DOSMesaMakeCurrent(Cur);
  701.     return GL_TRUE;
  702. }
  703.  
  704. // Initialize a window, create a rendering context for that window
  705. GLenum tkInitWindow(char *title)
  706. {
  707.     TKASSERT( NULL==tkhwnd      );
  708.     TKASSERT( NULL==tkhdc       );
  709.     TKASSERT( NULL==tkhrc       );
  710.     TKASSERT( NULL==tkhpalette  );
  711.  
  712.     return tkInitWindowAW(title, FALSE);
  713. }
  714.  
  715.  
  716. /******************************************************************************/
  717.  
  718. /*
  719.  * You cannot just call DestroyWindow() here.  The programs do not expect
  720.  * tkQuit() to return;  DestroyWindow() just sends a WM_DESTROY message
  721.  */
  722.  
  723. void tkQuit(void)
  724. {
  725.     exit(1);
  726. #if 0
  727.     DestroyThisWindow(tkhwnd);
  728.     ExitProcess(0);
  729. #endif
  730. }
  731.  
  732. /******************************************************************************/
  733.  
  734. #if defined( __WATCOMC__)
  735. void outpb(unsigned char byte);
  736. #pragma aux outpb = \
  737.     "mov dx,03c9h" \
  738.     "out dx,al" \
  739.     parm [ax] \
  740.     modify exact [dx];
  741. #endif
  742.  
  743. static unsigned char Rr,Gg,Bb,Cindex;
  744. void  set_onecolor(int cindex,int R,int G,int B)
  745. {
  746. #if defined( __WATCOMC__ )
  747.     outp(0x3c8,cindex);
  748.     outpb(R);
  749.     outpb(G);
  750.     outpb(B);
  751. #endif
  752.  
  753. #if defined( DJGPP )
  754.     Rr=R;Gg=G;Bb=B;
  755.     Cindex = cindex;
  756.  
  757.     asm ("
  758.         movw    $0x3c8,%dx
  759.         movw    _Cindex,%ax
  760.         outb    %al,%dx
  761.         inc    %dx
  762.         movb    _Rr,%al
  763.         outb    %al,%dx
  764.         movb    _Gg,%al
  765.         outb    %al,%dx
  766.         movb    _Bb,%al
  767.         outb    %al,%dx
  768.     ");
  769. #endif
  770. }
  771.  
  772. void tkSetOneColor(int index, float r, float g, float b)
  773. {
  774.     unsigned char R=r*63.0,G=g*63,B=b*63;
  775.  
  776.     set_onecolor(index,R,G,B);
  777.  
  778. #if 0
  779.     PALETTEENTRY PalEntry;
  780.     HPALETTE Palette;
  781.     if ( NULL != (Palette = CreateCIPalette( tkhdc )) )
  782.     {
  783.         PalEntry.peRed   = (BYTE)(r*(float)255.0 + (float)0.5);
  784.         PalEntry.peGreen = (BYTE)(g*(float)255.0 + (float)0.5);
  785.         PalEntry.peBlue  = (BYTE)(b*(float)255.0 + (float)0.5);
  786.         PalEntry.peFlags = 0;
  787.         SetPaletteEntries( Palette, index, 1, &PalEntry);
  788.         DelayPaletteRealization();
  789.     }
  790. #endif
  791. }
  792.  
  793. void tkSetFogRamp(int density, int startIndex)
  794. {
  795.     UINT n, i, j, k, intensity, fogValues, colorValues;
  796.  
  797.     fogValues = 1 << density ;
  798.     colorValues = 1 << startIndex ;
  799.     for( i = 0 ; i < colorValues; i++ ) {
  800.         for( j = 0 ; j < fogValues; j++ ) {
  801.             k = i * fogValues + j;
  802.  
  803.             intensity = i * fogValues + j * colorValues;
  804.  
  805.             if( intensity > 0xff )
  806.                 intensity = 0xff;
  807.  
  808.  
  809.             intensity/=4;
  810.  
  811.             set_onecolor(k,intensity,intensity,intensity);
  812.         }
  813.     }
  814. }
  815.  
  816. void tkSetGreyRamp(void)
  817. {
  818.     UINT Count,i;
  819.     float intensity;
  820.  
  821.     Count = tkGetColorMapSize();
  822.  
  823.     for( i = 0 ; i < Count ; i++ )
  824.     {
  825.         intensity = (float)(((double)i / (double)(Count-1)) * (double)255.0 + (double)0.5);
  826.         intensity/=4;
  827.         set_onecolor(i,intensity,intensity,intensity);
  828.     }
  829.  
  830. #if 0
  831.     HPALETTE CurrentPal;
  832.     PALETTEENTRY *Entries;
  833.     UINT Count, i;
  834.     float intensity;
  835.  
  836.     if ( NULL != (CurrentPal = CreateCIPalette( tkhdc )) )
  837.     {
  838.         Count   = GetPaletteEntries( CurrentPal, 0, 0, NULL );
  839.         Entries = AllocateMemory( Count * sizeof(PALETTEENTRY) );
  840.  
  841.         if ( NULL != Entries )
  842.         {
  843.             for (i = 0; i < Count; i++)
  844.             {
  845.                 intensity = (float)(((double)i / (double)(Count-1)) * (double)255.0 + (double)0.5);
  846.                 Entries[i].peRed =
  847.                 Entries[i].peGreen =
  848.                 Entries[i].peBlue = (BYTE) intensity;
  849.                 Entries[i].peFlags = 0;
  850.             }
  851.             SetPaletteEntries( CurrentPal, 0, Count, Entries );
  852.             FreeMemory( Entries );
  853.  
  854.             DelayPaletteRealization();
  855.         }
  856.     }
  857. #endif
  858. }
  859.  
  860. void tkSetRGBMap( int Size, float *Values )
  861. {
  862.     HPALETTE CurrentPal;
  863.     int i;
  864.     if ( NULL != (CurrentPal = CreateCIPalette( tkhdc )) )
  865.     {
  866.       for (i=0; i<Size; i++)
  867.         tkSetOneColor(i,Values[i*3],Values[i*3+1],Values[i*3+2]);
  868.     }
  869. }
  870.  
  871. /******************************************************************************/
  872.  
  873. void tkSwapBuffers(void)
  874. {
  875.   DOSMesaSwapBuffers();
  876. }
  877.  
  878. /******************************************************************************/
  879.  
  880. GLint tkGetColorMapSize(void)
  881. {
  882. #if 0
  883.     CreateCIPalette( tkhdc );
  884.  
  885.     if ( NULL == tkhpalette )
  886.         return( 0 );
  887.  
  888.     return( GetPaletteEntries( tkhpalette, 0, 0, NULL ) );
  889. #endif
  890.     return 256;
  891.  
  892. }
  893.  
  894. void tkGetMouseLoc(int *x, int *y)
  895. {
  896. #if 0
  897.     POINT Point;
  898.  
  899.     *x = 0;
  900.     *y = 0;
  901.  
  902.     GetCursorPos(&Point);
  903.  
  904.     /*
  905.      *  GetCursorPos returns screen coordinates,
  906.      *  we want window coordinates
  907.      */
  908.  
  909.     *x = Point.x - windInfo.x;
  910.     *y = Point.y - windInfo.y;
  911. #endif
  912. }
  913.  
  914. HWND tkGetHWND(void)
  915. {
  916.     return tkhwnd;
  917. }
  918.  
  919. HDC tkGetHDC(void)
  920. {
  921.     return tkhdc;
  922. }
  923. GLenum tkGetDisplayModePolicy(void)
  924. {
  925.     return windInfo.dmPolicy;
  926. }
  927.  
  928. GLint tkGetDisplayModeID(void)
  929. {
  930.     return windInfo.ipfd;
  931. }
  932.  
  933. GLenum tkGetDisplayMode(void)
  934. {
  935.     return windInfo.type;
  936. }
  937.  
  938.  
  939. /***********************************************************************
  940.  *                                                                     *
  941.  *  The Following functions are for our own use only. (ie static)      *
  942.  *                                                                     *
  943.  ***********************************************************************/
  944.  
  945. long tkWndProc(HWND hWnd, UINT message, DWORD wParam, LONG lParam)
  946. {
  947. #if 0
  948.     int key;
  949.     PAINTSTRUCT paint;
  950.     HDC hdc;
  951.  
  952.     switch (message)
  953.     {
  954.  
  955.     case WM_USER:
  956.  
  957.         if ( RealizePaletteNow( tkhdc, tkhpalette) > 0 )
  958.             ForceRedraw( hWnd );
  959.         return(0);
  960.  
  961.     case WM_SIZE:
  962.         windInfo.width  = LOWORD(lParam);
  963.         windInfo.height = HIWORD(lParam);
  964.  
  965.         if (ReshapeFunc)
  966.         {
  967.             (*ReshapeFunc)(windInfo.width, windInfo.height);
  968.  
  969.             ForceRedraw( hWnd );
  970.         }
  971.         return (0);
  972.  
  973.     case WM_MOVE:
  974.         windInfo.x = LOWORD(lParam);
  975.         windInfo.y = HIWORD(lParam);
  976.         return (0);
  977.  
  978.     case WM_PAINT:
  979.  
  980.         /*
  981.          *  Validate the region even if there are no DisplayFunc.
  982.          *  Otherwise, USER will not stop sending WM_PAINT messages.
  983.          */
  984.  
  985.         hdc = BeginPaint(tkhwnd, &paint);
  986.  
  987.         if (DisplayFunc)
  988.         {
  989.             (*DisplayFunc)();
  990.         }
  991.  
  992.         EndPaint(tkhwnd, &paint);
  993.         return (0);
  994.  
  995.     case WM_PALETTECHANGED:
  996.         if ( hWnd != (HWND) wParam )
  997.           RealizePaletteNow(tkhdc,tkhpalette);
  998.         return (0);
  999.     case WM_QUERYNEWPALETTE:
  1000.  
  1001.     // In the foreground!  Let RealizePaletteNow do the work--
  1002.     // if management of the static system color usage is needed,
  1003.     // RealizePaletteNow will take care of it.
  1004.  
  1005.         if ( NULL != tkhpalette )
  1006.         {
  1007.             if ( RealizePaletteNow(tkhdc, tkhpalette) > 0 )
  1008.                 ForceRedraw( hWnd );
  1009.  
  1010.             return (1);
  1011.         }
  1012.  
  1013.         return (0);
  1014.  
  1015.     case WM_ACTIVATE:
  1016.  
  1017.     // If the window is going inactive, the palette must be realized to
  1018.     // the background.  Cannot depend on WM_PALETTECHANGED to be sent since
  1019.     // the window that comes to the foreground may or may not be palette
  1020.     // managed.
  1021.  
  1022.         if ( LOWORD(wParam) == WA_INACTIVE )
  1023.         {
  1024.             if ( NULL != tkhpalette )
  1025.             {
  1026.             // Realize as a background palette.  Need to call
  1027.             // RealizePaletteNow rather than RealizePalette directly to
  1028.             // because it may be necessary to release usage of the static
  1029.             // system colors.
  1030.  
  1031.                 if ( RealizePaletteNow( tkhdc, tkhpalette) > 0 )
  1032.                     ForceRedraw( hWnd );
  1033.             }
  1034.         }
  1035.  
  1036.     // Allow DefWindowProc() to finish the default processing (which includes
  1037.     // changing the keyboard focus).
  1038.  
  1039.         break;
  1040.  
  1041.     case WM_MOUSEMOVE:
  1042.  
  1043.         if (MouseMoveFunc)
  1044.         {
  1045.             GLenum mask;
  1046.  
  1047.             mask = 0;
  1048.             if (wParam & MK_LBUTTON) {
  1049.                 mask |= TK_LEFTBUTTON;
  1050.             }
  1051.             if (wParam & MK_MBUTTON) {
  1052.                 mask |= TK_MIDDLEBUTTON;
  1053.             }
  1054.             if (wParam & MK_RBUTTON) {
  1055.                 mask |= TK_RIGHTBUTTON;
  1056.             }
  1057.  
  1058.             if ((*MouseMoveFunc)( LOWORD(lParam), HIWORD(lParam), mask ))
  1059.             {
  1060.                 ForceRedraw( hWnd );
  1061.             }
  1062.         }
  1063.         return (0);
  1064.  
  1065.     case WM_LBUTTONDOWN:
  1066.  
  1067.         SetCapture(hWnd);
  1068.  
  1069.         if (MouseDownFunc)
  1070.         {
  1071.             if ( (*MouseDownFunc)(LOWORD(lParam), HIWORD(lParam),
  1072.                  TK_LEFTBUTTON) )
  1073.             {
  1074.                 ForceRedraw( hWnd );
  1075.             }
  1076.         }
  1077.         return (0);
  1078.  
  1079.     case WM_LBUTTONUP:
  1080.  
  1081.         ReleaseCapture();
  1082.  
  1083.         if (MouseUpFunc)
  1084.         {
  1085.             if ((*MouseUpFunc)(LOWORD(lParam), HIWORD(lParam), TK_LEFTBUTTON))
  1086.             {
  1087.                 ForceRedraw( hWnd );
  1088.             }
  1089.         }
  1090.         return (0);
  1091.  
  1092.     case WM_MBUTTONDOWN:
  1093.  
  1094.         SetCapture(hWnd);
  1095.  
  1096.         if (MouseDownFunc)
  1097.         {
  1098.             if ((*MouseDownFunc)(LOWORD(lParam), HIWORD(lParam),
  1099.                     TK_MIDDLEBUTTON))
  1100.             {
  1101.                 ForceRedraw( hWnd );
  1102.             }
  1103.         }
  1104.         return (0);
  1105.  
  1106.     case WM_MBUTTONUP:
  1107.  
  1108.         ReleaseCapture();
  1109.  
  1110.         if (MouseUpFunc)
  1111.         {
  1112.             if ((*MouseUpFunc)(LOWORD(lParam), HIWORD(lParam),
  1113.                 TK_MIDDLEBUTTON))
  1114.             {
  1115.                 ForceRedraw( hWnd );
  1116.             }
  1117.         }
  1118.         return (0);
  1119.  
  1120.     case WM_RBUTTONDOWN:
  1121.  
  1122.         SetCapture(hWnd);
  1123.  
  1124.         if (MouseDownFunc)
  1125.         {
  1126.             if ((*MouseDownFunc)(LOWORD(lParam), HIWORD(lParam),
  1127.                 TK_RIGHTBUTTON))
  1128.             {
  1129.                 ForceRedraw( hWnd );
  1130.             }
  1131.         }
  1132.         return (0);
  1133.  
  1134.     case WM_RBUTTONUP:
  1135.  
  1136.         ReleaseCapture();
  1137.  
  1138.         if (MouseUpFunc)
  1139.         {
  1140.             if ((*MouseUpFunc)(LOWORD(lParam), HIWORD(lParam),
  1141.                 TK_RIGHTBUTTON))
  1142.             {
  1143.                 ForceRedraw( hWnd );
  1144.             }
  1145.         }
  1146.         return (0);
  1147.  
  1148.     case WM_KEYDOWN:
  1149.         switch (wParam) {
  1150.         case VK_SPACE:          key = TK_SPACE;         break;
  1151.         case VK_RETURN:         key = TK_RETURN;        break;
  1152.         case VK_ESCAPE:         key = TK_ESCAPE;        break;
  1153.         case VK_LEFT:           key = TK_LEFT;          break;
  1154.         case VK_UP:             key = TK_UP;            break;
  1155.         case VK_RIGHT:          key = TK_RIGHT;         break;
  1156.         case VK_DOWN:           key = TK_DOWN;          break;
  1157.         default:                key = GL_FALSE;         break;
  1158.         }
  1159.  
  1160.         if (key && KeyDownFunc)
  1161.         {
  1162.             GLenum mask;
  1163.  
  1164.             mask = 0;
  1165.             if (GetKeyState(VK_CONTROL)) {
  1166.                 mask |= TK_CONTROL;
  1167.             }
  1168.  
  1169.             if (GetKeyState(VK_SHIFT)) {
  1170.  
  1171.                 mask |= TK_SHIFT;
  1172.             }
  1173.  
  1174.             if ( (*KeyDownFunc)(key, mask) )
  1175.             {
  1176.                 ForceRedraw( hWnd );
  1177.             }
  1178.         }
  1179.         return (0);
  1180.  
  1181.     case WM_CHAR:
  1182.         if (('0' <= wParam && wParam <= '9') ||
  1183.             ('a' <= wParam && wParam <= 'z') ||
  1184.             ('A' <= wParam && wParam <= 'Z')) {
  1185.  
  1186.             key = wParam;
  1187.         } else {
  1188.             key = GL_FALSE;
  1189.         }
  1190.  
  1191.         if (key && KeyDownFunc) {
  1192.             GLenum mask;
  1193.  
  1194.             mask = 0;
  1195.  
  1196.             if (GetKeyState(VK_CONTROL)) {
  1197.                 mask |= TK_CONTROL;
  1198.             }
  1199.  
  1200.             if (GetKeyState(VK_SHIFT)) {
  1201.                 mask |= TK_SHIFT;
  1202.             }
  1203.  
  1204.             if ( (*KeyDownFunc)(key, mask) )
  1205.             {
  1206.                 ForceRedraw( hWnd );
  1207.             }
  1208.         }
  1209.         return (0);
  1210.  
  1211.     case WM_CLOSE:
  1212.         DestroyWindow(tkhwnd);
  1213.         return(0);
  1214.  
  1215.     case WM_DESTROY:
  1216.         CleanUp();
  1217.         PostQuitMessage(TRUE);
  1218.         return 0;
  1219.     }
  1220.     return(DefWindowProc( hWnd, message, wParam, lParam));
  1221. #endif
  1222.     return 1;
  1223. }
  1224.  
  1225. static HPALETTE CreateCIPalette( HDC Dc )
  1226. {
  1227. #if 0
  1228.     LOGPALETTE *LogicalPalette;
  1229.     HPALETTE StockPalette;
  1230.     UINT PaletteSize, StockPaletteSize, EntriesToCopy;
  1231.  
  1232.     if ( (Dc != NULL) && (NULL == tkhpalette) )
  1233.     {
  1234.                 PaletteSize = 256; //(Pfd.cColorBits >= 8) ? 256 : (1 << Pfd.cColorBits);
  1235.  
  1236.                 LogicalPalette = AllocateZeroedMemory( sizeof(LOGPALETTE) +
  1237.                                         (PaletteSize * sizeof(PALETTEENTRY)) );
  1238.  
  1239.                 if ( NULL != LogicalPalette )
  1240.                 {
  1241.                     LogicalPalette->palVersion    = 0x300;
  1242.                     LogicalPalette->palNumEntries = PaletteSize;
  1243.  
  1244.                     StockPalette     = GetStockObject(DEFAULT_PALETTE);
  1245.                     StockPaletteSize = GetPaletteEntries( StockPalette, 0, 0, NULL );
  1246.  
  1247.                     /*
  1248.                      *  start by copying default palette into new one
  1249.                      */
  1250.  
  1251.                     EntriesToCopy = StockPaletteSize < PaletteSize ?
  1252.                                         StockPaletteSize : PaletteSize;
  1253.  
  1254.                     GetPaletteEntries( StockPalette, 0, EntriesToCopy,
  1255.                                         LogicalPalette->palPalEntry );
  1256.  
  1257.                     /*
  1258.                      *  If we are taking possession of the system colors,
  1259.                      *  must guarantee that 0 and 255 are black and white
  1260.                      *  (respectively).
  1261.                      */
  1262.  
  1263.                     tkhpalette = CreatePalette(LogicalPalette);
  1264.  
  1265.                     FreeMemory(LogicalPalette);
  1266.  
  1267.                     RealizePaletteNow( Dc, tkhpalette);
  1268.                 }
  1269.             }
  1270.     return( tkhpalette );
  1271. #endif
  1272. }
  1273. static void
  1274. PrintMessage( const char *Format, ... )
  1275. {
  1276.     va_list ArgList;
  1277.     char Buffer[256];
  1278.  
  1279.     va_start(ArgList, Format);
  1280.     vsprintf(Buffer, Format, ArgList);
  1281.     va_end(ArgList);
  1282. }
  1283.  
  1284. static void DelayPaletteRealization( void )
  1285. {
  1286. #if 0
  1287.     MSG Message;
  1288.  
  1289.     TKASSERT(NULL!=tkhwnd);
  1290.  
  1291.     /*
  1292.      *  Add a WM_USER message to the queue, if there isn't one there already.
  1293.      */
  1294.  
  1295.     if (!PeekMessage(&Message, tkhwnd, WM_USER, WM_USER, PM_NOREMOVE) )
  1296.     {
  1297.         PostMessage( tkhwnd, WM_USER, 0, 0);
  1298.     }
  1299. #endif
  1300. }
  1301.  
  1302. /******************************Public*Routine******************************\
  1303. * RealizePaletteNow
  1304. *
  1305. * Select the given palette in background or foreground mode (as specified
  1306. * by the bForceBackground flag), and realize the palette.
  1307. *
  1308. * If static system color usage is set, the system colors are replaced.
  1309. *
  1310. * History:
  1311. *  26-Apr-1994 -by- Gilman Wong [gilmanw]
  1312. * Wrote it.
  1313. \**************************************************************************/
  1314.  
  1315. static long RealizePaletteNow( HDC Dc, HPALETTE Palette)
  1316. {
  1317. #if 0
  1318.     long Result = -1;
  1319.     TKASSERT( NULL!=Dc      );
  1320.     TKASSERT( NULL!=Palette );
  1321.     if ( NULL != SelectPalette( Dc, Palette, FALSE ) )
  1322.     {
  1323.       Result = RealizePalette( Dc );
  1324.       DOSMesaPaletteChange(Palette);
  1325.     }
  1326.     return( Result );
  1327.  
  1328. #endif
  1329. }
  1330.  
  1331. static void ForceRedraw( HWND Window )
  1332. {
  1333. #if 0
  1334.     MSG Message;
  1335.  
  1336.     if (!PeekMessage(&Message, Window, WM_PAINT, WM_PAINT, PM_NOREMOVE) )
  1337.     {
  1338.         InvalidateRect( Window, NULL, FALSE );
  1339.     }
  1340. #endif
  1341. }
  1342. static void DestroyThisWindow( HWND Window )
  1343. {
  1344. #if 0
  1345.     if ( NULL != Window )
  1346.     {
  1347.         DestroyWindow( Window );
  1348.     }
  1349. #endif
  1350. }
  1351.  
  1352. /*
  1353.  *  This Should be called in response to a WM_DESTROY message
  1354.  */
  1355.  
  1356. static void CleanUp( void )
  1357. {
  1358.  
  1359. // Be really nice and reset global values.
  1360.     tkhwnd        = NULL;
  1361.     tkhdc         = NULL;
  1362.     tkhpalette    = NULL;
  1363.  
  1364.     ExposeFunc    = NULL;
  1365.     ReshapeFunc   = NULL;
  1366.     IdleFunc      = NULL;
  1367.     DisplayFunc   = NULL;
  1368.     KeyDownFunc   = NULL;
  1369.     MouseDownFunc = NULL;
  1370.     MouseUpFunc   = NULL;
  1371.     MouseMoveFunc = NULL;
  1372. }
  1373.  
  1374. static void *AllocateMemory( size_t Size )
  1375. {
  1376.     return( malloc( Size ) );
  1377. }
  1378.  
  1379. static void *AllocateZeroedMemory( size_t Size )
  1380. {
  1381.     void *temp = malloc(Size);
  1382.     memset(temp,0,Size);
  1383.     return( temp );
  1384. }
  1385.  
  1386.  
  1387. static void FreeMemory( void *Chunk )
  1388. {
  1389.     TKASSERT( NULL!=Chunk );
  1390.  
  1391.     free( Chunk );
  1392. }
  1393.